// Protocol Buffers for GWT
// Copyright 2010 Thomas Broyer  All rights reserved.
// http://code.google.com/p/protobuf-gwt/
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are
// met:
//
//     * Redistributions of source code must retain the above copyright
// notice, this list of conditions and the following disclaimer.
//     * Redistributions in binary form must reproduce the above
// copyright notice, this list of conditions and the following disclaimer
// in the documentation and/or other materials provided with the
// distribution.
//     * Neither the name of Google Inc. nor the names of its
// contributors may be used to endorse or promote products derived from
// this software without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

package net.ltgt.gwt.protobuf.compiler.client;

import com.google.protobuf.Descriptors.EnumDescriptor;
import com.google.protobuf.Descriptors.EnumValueDescriptor;
import com.google.protobuf.compiler.JavaHelpers;

import java.io.IOException;
import java.io.Writer;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * @author t.broyer@ltgt.net Thomas Broyer
 * Based on the initial work of:
 * @author kenton@google.com Kenton Varda
 * Based on original Protocol Buffers design by
 * Sanjay Ghemawat, Jeff Dean, and others.
 */
public class EnumGenerator extends Generator {

  private final EnumDescriptor descriptor;
  private final List<EnumValueDescriptor> canonicalValues;
  private final List<String> aliases;

  public EnumGenerator(EnumDescriptor descriptor) {
    super(descriptor.getName());
    this.descriptor = descriptor;

    ArrayList<EnumValueDescriptor> canonicalValues = new ArrayList<EnumValueDescriptor>();
    ArrayList<String> aliases = new ArrayList<String>();

    for (EnumValueDescriptor enumValue : descriptor.getValues()) {
      EnumValueDescriptor canonicalValue = descriptor.findValueByNumber(enumValue.getNumber());
      if (enumValue == canonicalValue) {
        canonicalValues.add(enumValue);
      } else {
        aliases.add(enumValue.getName() + " = " + canonicalValue.getName());
      }
    }

    this.canonicalValues = Collections.unmodifiableList(canonicalValues);
    this.aliases = Collections.unmodifiableList(aliases);
  }

  @Override
  public void generate(Writer output) throws IOException {
    output.write("public enum " + descriptor.getName() + "\n");
    if (JavaHelpers.hasDescriptorMethods(descriptor)) {
      output.write("    implements com.google.protobuf.ProtocolMessageEnum {\n");
    } else {
      output.write("    implements com.google.protobuf.Internal.EnumLite {\n");
    }
    // indent

    for (EnumValueDescriptor value : canonicalValues) {
      output.write(value.getName() + " {\n");
      output.write("  public int getNumber() { return " + value.getNumber()
          + "; }\n");
      if (JavaHelpers.hasDescriptorMethods(descriptor)) {
        output.write("  public com.google.protobuf.Descriptors.EnumValueDescriptor getValueDescriptor() {\n");
        output.write("    return getDescriptor().getValues().get(" + value.getIndex() + ");\n");
        output.write("  }\n");
      }
      output.write("},");
    }
    output.write(";\n");
    output.write("\n");

    String prefix = "public static final " + descriptor.getName() + " ";
    for (String alias : aliases) {
      output.write(prefix + alias + ";\n");
    }

    output.write("\n");
    output.write("public static " + descriptor.getName()
        + " valueOf(int value) {\n");
    output.write("  switch (value) {\n");
    for (EnumValueDescriptor enumValue : canonicalValues) {
      output.write("  case " + enumValue.getNumber() + ": return "
      // + descriptor.getName() + "."
          + enumValue.getName() + ";\n");
    }
    output.write(" default: return null;\n");
    output.write("  }\n");
    output.write("}\n");

    // descriptor
    if (JavaHelpers.hasDescriptorMethods(descriptor)) {
      output.write("public com.google.protobuf.Descriptors.EnumDescriptor\n");
      output.write("   getDescriptorForType() {\n");
      output.write("  return getDescriptor();\n");
      output.write("}\n");
      output.write("\n");
      output.write("public static final com.google.protobuf.Descriptors.EnumDescriptor\n");
      output.write("   getDescriptor() {\n");
      output.write("  return Descriptor.INSTANCE;\n");
      output.write("}\n");
      output.write("\n");
      output.write("private static final class Descriptor extends com.google.protobuf.Descriptors.EnumDescriptor {\n");
      output.write("  public static final Descriptor INSTANCE = new Descriptor();\n");
      output.write("\n");
      output.write("  public int getIndex() { return " + descriptor.getIndex()
          + "; }\n");
      output.write("  public String getName() { return \"" + descriptor.getName()
          + "\"; }\n");
      if (descriptor.getContainingType() != null) {
        output.write("  public com.google.protobuf.Descriptors.Descriptor getContainingType() {\n");
        output.write("    return "
            + JavaHelpers.getClassName(descriptor.getContainingType())
            + ".getDescriptor();\n");
        output.write("  }\n");
      } else {
        output.write("  public String getFullName() { return \""
            + descriptor.getFullName() + "\"; }\n");
      }
      output.write("  public com.google.protobuf.Descriptors.FileDescriptor getFile() {\n");
      output.write("    return " + JavaHelpers.getClassName(descriptor.getFile()) + ".getDescriptor();\n");
      output.write("  }\n");
      output.write("  protected com.google.protobuf.Descriptors.EnumValueDescriptor[] createValues() {\n");
      output.write("    return new com.google.protobuf.Descriptors.EnumValueDescriptor[] {\n");
      for (EnumValueDescriptor value : descriptor.getValues()) {
        output.write("      new com.google.protobuf.Descriptors.EnumValueDescriptor(this, "
            + value.getIndex()
            + ", \""
            + value.getName()
            + "\", "
            + value.getNumber() + "),\n");
      }
      output.write("    };\n");
      output.write("  }\n");
      output.write("}\n");
    }

    output.write("\n");
    output.write("// @@protoc_insertion_point(enum_scope:"
        + descriptor.getFullName() + ")\n");

    output.write("}\n");
  }
}
